//
//  CYFinanceTitleCell.m
//  cyjf
//
//  Created by tauCross on 2017/10/20.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYFinanceTitleCell.h"

#import <UIImageView+WebCache.h>



@implementation CYFinanceTitleCellObject

@end



@interface CYFinanceTitleCell ()

@property(nonatomic, strong)UIImageView *iconImageView;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UIButton *moreSellOutButton;

@property(nonatomic, strong)CYFinanceTitleCellObject *object;

@end


@implementation CYFinanceTitleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.iconImageView = [[UIImageView alloc] init];
        self.iconImageView.size = CGSizeMake(20, 20);
        [self.contentView addSubview:self.iconImageView];
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textColor = HEXCOLOR(@"666666");
        self.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
        [self.contentView addSubview:self.titleLabel];
        
        self.moreSellOutButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 78, 38)];
        [self.moreSellOutButton setTitle:@"更多" forState:UIControlStateNormal];
        [self.moreSellOutButton setImage:CYImage(@"ico_rightarrow_readmore") forState:UIControlStateNormal];
        [self.moreSellOutButton setTitleColor:HEXCOLOR(@"999999") forState:UIControlStateNormal];
        [self.moreSellOutButton.titleLabel setFont:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular]];
        self.moreSellOutButton.titleEdgeInsets = UIEdgeInsetsMake(0, - self.moreSellOutButton.imageView.width - 1, 0, self.moreSellOutButton.imageView.width + 1);
        self.moreSellOutButton.imageEdgeInsets = UIEdgeInsetsMake(0, self.moreSellOutButton.titleLabel.intrinsicContentSize.width + 1, 0, - self.moreSellOutButton.titleLabel.intrinsicContentSize.width - 1);
        [self.moreSellOutButton addTarget:self action:@selector(onMoreSellOutButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.moreSellOutButton];
    }
    return self;
}

+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 38;
}

- (BOOL)shouldUpdateCellWithObject:(CYFinanceTitleCellObject *)object
{
    self.object = object;
    
    self.moreSellOutButton.hidden = object.type != CYFinanceTitleTypeSellOut;
    [self.iconImageView sd_setImageWithURL:object.image.tc_url];
    self.titleLabel.text = object.title;

    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.iconImageView.left = 20;
    self.iconImageView.centerY = self.contentView.height / 2;
    
    [self.titleLabel sizeToFit];
    self.titleLabel.centerY = self.contentView.height / 2;
    self.titleLabel.left = 47;
    
    self.moreSellOutButton.centerY = self.contentView.height / 2;
    self.moreSellOutButton.right = self.contentView.width;
}

#pragma mark - Control Events
- (void)onMoreSellOutButtonTouchUpInside
{
    if(self.object.moreSellOutAction)
    {
        self.object.moreSellOutAction();
    }
}

@end
